/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-06-29
 * Time: 12:43
 */
class Solution20 {
    int m,n,num;
    int[] dx = {1,-1,0,0};
    int[] dy = {0,0,1,-1};


    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        m = image.length;
        n = image[0].length;
        num = image[sr][sc];

        //特殊处理
        if(num == color) {
            return image;
        }

        dfs(image,sr,sc,color);
        return image;
    }

    void dfs(int[][] image,int i,int j ,int color){
        //先把这个改成颜色
        image[i][j] = color;

        for(int k = 0; k < 4; k++) {
            int x = i + dx[k], y = j + dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && image[x][y] == num) {
                dfs(image,x,y,color);
            }
        }
    }
}
